package cn.tedu.coolshark.Controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @author 作者：HeShaoShuai
 * @Email 邮箱：2569819541@qq.com
 * @Date 日期：2023/5/19 9:24
 * @Package 包名：cn.tedu.coolshark.Controller
 * @Project 项目名：JavaSE
 * @Version 1.0
 */
@RestController
public class UploadController {

	//@Value的作用是给变量赋值 ${xxx}写法会去application.properties里面找到对应的配置信息
	@Value("${imagesPath}")
	private String dirPath;

	@RequestMapping("/upload")
	public String upload(MultipartFile picFile) throws IOException {
		String fileName= picFile.getOriginalFilename();
		String suffix = fileName.substring(fileName.lastIndexOf("."));
		fileName = UUID.randomUUID()+suffix;

		String datePath = new SimpleDateFormat("/yyyy/MM/dd/").format(new Date());
		File dirFile = new File(dirPath+datePath);
		if (!dirFile.exists()){
			dirFile.mkdirs();
		}
		picFile.transferTo(new File(dirPath+datePath+fileName));
		return datePath+fileName;
	}
	@RequestMapping("/remove")
	public void remove(String url){
		new File(dirPath+url).delete();
	}
}